package websocket.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import websocket.entity.ReceiveUVN;
import websocket.enum_.MsgType;
import websocket.service.ChuanKouService;
import websocket.socket_.SocketMsg;
import websocket.socket_.WebSocketServer;
import websocket.util.DateUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author nieruijie
 * @date 2022/4/18 22:21
 */
@RestController
@RequestMapping("/receive")
public class ReceiveUVNController {

    private static final Logger log = LoggerFactory.getLogger(ReceiveUVNController.class);

    @RequestMapping("/{msg}")
    public void receiveMsg(@PathVariable("msg") String msg) {
        try {
            SocketMsg socketMsg = new SocketMsg(msg, MsgType.INFO);
            WebSocketServer.sendInfo(socketMsg, "nrj");
            //System.err.println("消息发送成功,时间:" + DateUtil.formatNow());
        } catch (IOException e) {
            System.err.println("消息发送失败,时间:" + DateUtil.formatNow());
            e.printStackTrace();
        }
    }

    @PostMapping
    public void receive(@RequestBody ReceiveUVN receiveUVN, HttpServletRequest request) {
        try {
            SocketMsg socketMsg = new SocketMsg(JSONObject.toJSONString(receiveUVN), MsgType.INFO);
            WebSocketServer.sendInfo(socketMsg, "nrj");
            //log.info("消息发送成功,【无人机信息】时间:" + DateUtil.formatNow());
        } catch (IOException e) {
            e.printStackTrace();
            log.error("消息发送失败,【无人机信息】时间:" + DateUtil.formatNow());
        }
    }
}
















